﻿//
// GNS_RankLayer.cpp
// GameHall
//
// Created by 梁晓强 on 16/2/12.
//
//

#include "GNS_RankLayer.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GnsClientFrame.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

enum{
	BTN_BACK,
	BTN_GAME1,		 
	BTN_GAME2,		   
	BTN_DAY_RANK,
	BTN_MOTH_RANK,
	BTN_ALL_RANK,
};

GNS_RankLayer::GNS_RankLayer()
{
 
}

GNS_RankLayer::~GNS_RankLayer()
{
    
}

GNS_RankLayer* GNS_RankLayer::createLayer()
{
    auto pGNS_RankLayer = GNS_RankLayer::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_RankLayer);
    return pGNS_RankLayer;
}

bool GNS_RankLayer::init()
{
    if ( !GNS_YJFLayer::init() )
    {
        return false;
    }
    return true;
}
void GNS_RankLayer::SetNodeName(std::string CsbName)
{
    m_RootNode = CSLoader::createNode(CsbName);
	m_RootNode->setContentSize(winsize);
	m_RootNode->setAnchorPoint(Vec2(0.5f, 0.5f));
	ui::Helper::doLayout(m_RootNode);
	addChild(m_RootNode, 11);

	auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_4"));
    pBackBtn->setTag(BTN_BACK);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	  
	m_btnGame1 = static_cast<Button*>(m_RootNode->getChildByName("Game1"));
	m_btnGame1->setTag(BTN_GAME1);
	m_btnGame1->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	m_btnGame1->setVisible(false);

	m_btnGame2 = static_cast<Button*>(m_RootNode->getChildByName("Game2"));
	m_btnGame2->setTag(BTN_GAME2);
	m_btnGame2->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	m_btnGame2->setVisible(false);

	 //天排行
	m_btnRankByDay = static_cast<Button*>(m_RootNode->getChildByName("DayRank"));
	m_btnRankByDay->setTag(BTN_DAY_RANK);
	m_btnRankByDay->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	m_btnRankByDay->setVisible(false);

	  //月排行
	m_btnRankByMonth = static_cast<Button*>(m_RootNode->getChildByName("MonRank"));
	m_btnRankByMonth->setTag(BTN_MOTH_RANK);
	m_btnRankByMonth->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	m_btnRankByMonth->setVisible(false);

	 // 总排行
	m_btnRankByAll = static_cast<Button*>(m_RootNode->getChildByName("AllRank"));
	m_btnRankByAll->setTag(BTN_ALL_RANK);
	m_btnRankByAll->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	m_btnRankByAll->setVisible(false);

	m_RankListView = static_cast<ListView*>(m_RootNode->getChildByName("ListView_2"));
	m_RankListView->removeAllItems();	
}

void GNS_RankLayer::ShowRankList(bool isShow)
{
	if (isShow)
	{
		if (m_RankListView)
		{
			m_RankListView->removeAllItems();
		}
		//Show(true);
		map<int, CMD_SC_GetRanking> * rankDate = GNS_PlayerMgr::GetInstance()->getRankList();

		map<int, CMD_SC_GetRanking>::iterator mInfo = GNS_PlayerMgr::GetInstance()->getRankList()->begin();
		map<int, CMD_SC_GetRanking>::iterator it = GNS_PlayerMgr::GetInstance()->getRankList()->begin();

		//首先显示自己的排名
		for (; it != rankDate->end(); ++it)
		{
			if (it->second.llUserId == GNS_PlayerMgr::GetInstance()->getMyUserIdx())
			{
				createItem(it->second.llPlayInGold, "我", it->second.nRankNo, it->second.szDataTime, it->second.llSysLogoID, it->second.llUserId);
				break;
			}
		}
		if (it == rankDate->end())
		{
			createItem(GNS_PlayerMgr::GetInstance()->getMyGameGold(), "我", 10001, "", GNS_PlayerMgr::GetInstance()->getMyLogoID(), GNS_PlayerMgr::GetInstance()->getMyUserIdx());
		}

		int nIndex = 1;
		for (; mInfo != rankDate->end(); ++mInfo, ++nIndex)
		{
			//if (mInfo->second.llUserId == GNS_PlayerMgr::GetInstance()->getMyUserIdx())
				//continue;
			createItem(mInfo->second.llPlayInGold, mInfo->second.szUserName, mInfo->second.nRankNo, mInfo->second.szDataTime, mInfo->second.llSysLogoID, mInfo->second.llUserId);
		}
		//m_RankListView->getItem(0)->getContentSize();
		int item_height = 100;  //item的默认宽度是100
		if (m_RankListView->getItem(0))
		{
			item_height = m_RankListView->getItem(0)->getContentSize().height;
		}
		int temp_height = ((rankDate->size() + 1) * item_height) > 320 ? ((rankDate->size() + 1) * item_height) : 320;
		m_RankListView->setInnerContainerSize(Size(780, temp_height));

		rankDate->clear();   //用完之后要清理一次
	}
	else
	{
		Show(false);
	}
	
}

void GNS_RankLayer::createItem(long long gold, const char * name, long long rank, const char * time, long long headId, long long userId)
{
	auto itemList = Button::create("rankingcell.png", "rankingcell.png");
	itemList->ignoreAnchorPointForPosition(false);
	itemList->setAnchorPoint(Vec2(0,0));
	m_RankListView->addChild(itemList);

	itemList->setTag(0);

	auto RankSp = Sprite::create("1.png");
	RankSp->setPosition(Vec2(44.88,47.99));
	itemList->addChild(RankSp);

	auto RankText = Text::create();       //排名
	RankText->setPosition(Vec2(45.02, 49.10));
	RankText->setFontSize(20);
	RankText->setTextColor(Color4B(0,0,0,255));
	auto str = StringUtils::format("%lld", rank);
	RankText->setString(GTOU(str.c_str()));
	if (rank == 10001)
	{
		str = "不入流";
		RankText->setString(str.c_str());
		RankSp->setVisible(false);
	}
	itemList->addChild(RankText);

	auto headBg = Sprite::create("Pictureframe.png");
	headBg->setPosition(Vec2(120.05,50.00));
	itemList->addChild(headBg);

	auto strId = StringUtils::format("face_%lld.png", headId);
	auto headP = Sprite::create(strId);   //头像ID
	headP->setScale(0.8);
	headP->setPosition(Vec2(120.05, 50.00));
	itemList->addChild(headP);

	auto nameText = Text::create();    //昵称
	nameText->setPosition(Vec2(240,70));
	nameText->setFontSize(20);
	nameText->setAnchorPoint(Vec2(0, 0.5));
	nameText->setString(GTOU(name));
	itemList->addChild(nameText);

	auto goldT = Sprite::create("friendscion.png");
	goldT->setPosition(Vec2(250,30));
	itemList->addChild(goldT);

	auto goldText = Text::create();    //金币数量
	goldText->setPosition(Vec2(280, 18));
	goldText->setAnchorPoint(Vec2(0,0));
	auto pgold = StringUtils::format("%lld", gold);
	goldText->setString(GTOU(pgold.c_str()));
	goldText->setFontSize(20);
	itemList->addChild(goldText);

	auto timeText = Text::create();     //时间
	timeText->setPosition(Vec2(516.37, 50));
	timeText->setString(GTOU(time));
	timeText->setFontSize(20);
	itemList->addChild(timeText);

	auto btnAdd = Button::create("addfriend_button.png", "f1.png");
	btnAdd->addTouchEventListener(CC_CALLBACK_2(GNS_RankLayer::BtnCallBack, this));
	btnAdd->setPosition(Vec2(686.41,50));
	btnAdd->setTag(0);
	if (userId == GNS_PlayerMgr::GetInstance()->getMyUserIdx())
	{
		btnAdd->setVisible(false);
		nameText->setString("我");
	}
	itemList->addChild(btnAdd);
	return;
}

void GNS_RankLayer::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();

		GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
		assert(pGnsClientFrame);

        switch (BtnTag) {
            case BTN_BACK:
            {
				Show(false);
				return;
                //break;
            }
			case BTN_ALL_RANK:
			{
				m_btnRankByAll->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
				m_btnRankByDay->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_btnRankByMonth->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_RankType = 3;
				break;
			}
			case BTN_MOTH_RANK:
			{
				m_btnRankByMonth->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
				m_btnRankByDay->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_btnRankByAll->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_RankType = 2;
				break;
			}
			case BTN_DAY_RANK:
			{
				m_btnRankByDay->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
				m_btnRankByMonth->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_btnRankByAll->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_RankType = 1;
				break;
			}
			case BTN_GAME1:
			{
				m_btnGame1->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
				m_btnGame2->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_GameId = 1;
				break;
			}
			case BTN_GAME2:
			{
				m_btnGame2->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
				m_btnGame1->setBrightStyle(Widget::BrightStyle::NORMAL);
				m_GameId = 10;
				break;
			}
            default:
                break;
        }
		CMD_CS_GetRanking data;
		data.nGameId = m_GameId;
		data.nType = m_RankType;
		pGnsClientFrame->sendMsg(HALLSERVER, MAIN_MSG_PLAZA_GOLD_RANKING, SUB_CSG_GOLD_RANKING, (char*)&data, sizeof(data));
    }
}

void GNS_RankLayer::setButtonState()
{
	m_GameId = 10;
	m_RankType = 1;
	m_btnGame2->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
	m_btnRankByDay->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
	m_btnGame1->setBrightStyle(Widget::BrightStyle::NORMAL);
	m_btnRankByMonth->setBrightStyle(Widget::BrightStyle::NORMAL);
	m_btnRankByAll->setBrightStyle(Widget::BrightStyle::NORMAL);
}
